

#include "connectionoptions.h"

namespace freedatabase
{

	Persistent<Function> ConnectionOptions::constructor;

	ConnectionOptions::ConnectionOptions()
	{
	}

	ConnectionOptions::~ConnectionOptions()
	{
	}

	void ConnectionOptions::Init(v8::Local<v8::Object> exports)
	{
		Isolate *isolate = exports->GetIsolate();

		Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
		tpl->SetClassName(String::NewFromUtf8(isolate, "ConnectionOptions"));
		tpl->InstanceTemplate()->SetInternalFieldCount(1);

		NODE_SET_PROTOTYPE_METHOD(tpl, "setHost", SetHost);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setPort", SetPort);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setUsername", SetUsername);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setPassword", SetPassword);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setDbName", SetDbName);

		constructor.Reset(isolate, tpl->GetFunction());
		exports->Set(String::NewFromUtf8(isolate, "ConnectionOptions"), tpl->GetFunction());
	}
	void ConnectionOptions::New(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();

		if (args.IsConstructCall())
		{
			ConnectionOptions *connOpts = new ConnectionOptions();
			connOpts->Wrap(args.This());
			args.GetReturnValue().Set(args.This());
		}
		else
		{
			Local<Function> cons = Local<Function>::New(isolate, constructor);
			args.GetReturnValue().Set(cons->NewInstance());
		}
	}

	void ConnectionOptions::SetPort(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsNumber())
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		ConnectionOptions *connOpts = ObjectWrap::Unwrap<ConnectionOptions>(args.Holder());
		connOpts->_port = args[0]->NumberValue();
	}

	void ConnectionOptions::SetHost(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		ConnectionOptions *connOpts = ObjectWrap::Unwrap<ConnectionOptions>(args.Holder());

		Local<String> lstr = args[0]->ToString();
		char *str = new char[lstr->Utf8Length() + 1];
		lstr->WriteUtf8(str, lstr->Utf8Length() + 1);
		connOpts->_host = string(str);
		free(str);
	}
	void ConnectionOptions::SetUsername(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		ConnectionOptions *connOpts = ObjectWrap::Unwrap<ConnectionOptions>(args.Holder());

		Local<String> lstr = args[0]->ToString();
		char *str = new char[lstr->Utf8Length() + 1];
		lstr->WriteUtf8(str, lstr->Utf8Length() + 1);
		connOpts->_username = string(str);
		free(str);
	}

	void ConnectionOptions::SetPassword(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		ConnectionOptions *connOpts = ObjectWrap::Unwrap<ConnectionOptions>(args.Holder());

		Local<String> lstr = args[0]->ToString();
		char *str = new char[lstr->Utf8Length() + 1];
		lstr->WriteUtf8(str, lstr->Utf8Length() + 1);
		connOpts->_password = string(str);
		free(str);
	}

	void ConnectionOptions::SetDbName(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		ConnectionOptions *connOpts = ObjectWrap::Unwrap<ConnectionOptions>(args.Holder());

		Local<String> lstr = args[0]->ToString();
		char *str = new char[lstr->Utf8Length() + 1];
		lstr->WriteUtf8(str, lstr->Utf8Length() + 1);
		connOpts->_dbname = string(str);

		free(str);
	}
}